$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'book/subject/list',
        datatype: "json",
        colModel: [			
        	{ label: 'id', name: 'id',index:"id", key: true,width:80,align:'center' },
			{ label: '主题编号', name: 'topicalCode',index:"topical_code",width:100 ,align:'center'},
			{ label: '主题名称', name: 'topicalName',index:"topical_name",width:150,align:'center' },
			{ label: '主题简介', name: 'description',index:"description" ,width:350,align:'center' },
			{ label: '操作', name: 'id', width: 150 ,formatter:function(cellvalue, options, rowObject){  
				return "<a onclick=\"updateSubjectHtml("+rowObject.id+",\'"+rowObject.topicalName+"\',"+rowObject.description+")\">编辑</a>    <a  onclick=\"deleteSubject("+rowObject.id+")\">删除</a>";
			},sortable:false,align:'center'}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        autoScroll:true,
        shrinkToFit:false,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        }
        
    });
    /*创建jqGrid的操作按钮容器*/
    /*可以控制界面上增删改查的按钮是否显示*/
    //jQuery("#jqGrid").jqGrid('navGrid', '#jqGridPager', {edit : true,add : true,del : true});
    //表格宽度自适应
    $(function(){
       $(window).resize(function(){
          $("#jqGrid").setGridWidth($(window).width());
       });
    });
});


var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userName: null,
			mobileNo: null,
			statusCash: null
		},
		showList: true,
		title: null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		addSubjectHtml:function(){
			layer.open({
				type : 2,
				title : '主题创建',
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '400', '260' ],
				content : './book-subject-create.html'
			});
		},
		updateSubjectHtml:function(id,topicalName,description){
			console.log(topicalName);
		    arr = [];
		    arr.push(id);
		    arr.push(topicalName);
		    arr.push(description);
			layer.open({
				type : 2,
				title : '主题编辑',
				maxmin : true,
				shadeClose : true, // 点击遮罩关闭层
				area : [ '400', '260' ],
				content : './book-subject-create.html?id='+id+'&topicalName='+topicalName+'&description='+description,
				success: function (layero, index) {
					var body = layer.getChildFrame('body',index);//建立父子联系
					var iframeWin = window[layero.find('iframe')[0]['name']];
					var inputList = body.find('input');
		            for(var j = 0; j< inputList.length; j++){
		                $(inputList[j]).val(arr[j]);
		            }
			      }
			});
		},
		deleteSubject: function(id){
			var jsonData = {"id":id};
	    	ajaxPost('book/subject/deleteSubject',jsonData,'主题删除成功');
	    	layer.close(layer.index);
		},
		saveOrUpdateSubject: function(){
			layer.confirm('添加用户:<br/>请输入用户手机号：<input id="mobileNo" type="text" />', {
				btn : [ '提交', '取消' ]
					//按钮
			}, function() {
				var mobileNo = document.getElementById("mobileNo").value;
				
				var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
		        if (!myreg.test(mobileNo)) {  
		            layer.msg("请输入正确手机号。");
		        }
				var jsonData = {"mobileNo":mobileNo};
				ajaxPost('client/manager/saveOrUpdateSubject',jsonData,'添加成功');
			}, function() {
				layer.close(layer.index);
			});
		},
		reload: function (event) {
			//eg
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'mobileNo' : vm.q.mobileNo,
					'userName' : vm.q.userName,
					'cashStatus' : vm.q.statusCash
				},
				page : page
			}).trigger("reloadGrid");
		}
	}
});